﻿using UnityEngine;
using FairyGUI;
using Com.ZX.ViewModel;
using Com.ZX.Manager;
//name 
namespace Com.ZX.View
{
    /// <summary>
    /// UI基类
    /// </summary>
    /// <typeparam name="TModel">待绑定的ViewModel类</typeparam>
    public abstract class BaseUIView<TViewModel> : Window, IBase, IView 
        where TViewModel :BaseViewModel,new()
	{
        public BaseUIView()
        { }
        private IView _cacheView;
		/// <summary>
		/// Get the cache of gameobject
		/// </summary>
		public IView cacheView
		{
			get
			{
				if (_cacheView == null)
					_cacheView = this;
				return _cacheView;
			}
		}
        public bool IsLock = false;
		//protected bool isShown = false;

		//private Transform _cacheTransForm;
		///// <summary>
		///// Get the cache of transform
		///// </summary>
		//public Transform cacheTransForm
		//{
		//	get
		//	{
		//		if (_cacheTransForm == null)
		//			_cacheTransForm = this.transform;
		//		return _cacheTransForm;
		//	}
		//}


		protected EnumObjState _state = EnumObjState.None;
		/// <summary>
		/// 状态修改事件
		/// </summary>
		public event StateChangeEvent StateChanged;
		public EnumObjState State
		{
			protected get { return _state; }
			set
			{
				EnumObjState oldState = _state;
				_state = value;
				if (StateChanged != null)
					StateChanged(this, _state, oldState);
			}
		}

        public readonly BindableProperty<BaseViewModel> BaseViewModelProperty = new BindableProperty<BaseViewModel>();

        /// <summary>
        /// ViewModel 上下文
        /// </summary>
        public BaseViewModel BindContext
        {
            get
            {
                return BaseViewModelProperty.Value;
            }

            set
            {
                BaseViewModelProperty.Value = value;
            }
        }

        protected TViewModel ViewModel { get { return (TViewModel)BindContext; } }
        protected TViewModel OldViewModel { get; private set; }
        
        #region 供子类重写


        public abstract EnumUIType GetUIType();

        protected override void OnInit()
        {
            base.OnInit();
            this.onInit();
        }
        public void onInit()
		{
            //throw new NotImplementedException();
            this.State = EnumObjState.Initializing;
            this.BaseViewModelProperty.OnValueChanged += onBindPropertyChanged;
            ViewModelManager.inst.RegistModel<TViewModel>(this); //注册模块,把ViewModel绑定到View中
        }

		public virtual void onDestroy()
		{
			this.State = EnumObjState.None;
			//throw new NotImplementedException();
		}

        protected virtual void onBindPropertyChanged(BaseViewModel oldM, BaseViewModel newM)
        {
            OldViewModel = (TViewModel)oldM;
        }
        

		/// <summary>
		/// 释放UI资源
		/// </summary>
		protected virtual void onRelease()
		{
            this.Dispose();
		}
		/// <summary>
		/// 加载数据
		/// </summary>
		protected virtual void onLoadData()
		{
		}
		
		/// <summary>
		/// 设置UI
		/// </summary>
		/// <param name="uiParms"></param>
		protected virtual void SetUI(params object[] uiParms) 
		{
			this.State = EnumObjState.Loading;
		}

        protected override void OnShown()
        {
            base.OnShown();
        }

        protected override void OnHide()
        {
            IsLock = true;
            base.OnHide();
        }
        protected override void DoShowAnimation()
        {
            base.DoShowAnimation();
        }
        protected override void DoHideAnimation()
        {
            base.DoHideAnimation();
        }
        //protected override void CreateDisplayObject()
        //{
        //    base.CreateDisplayObject();
        //}

        #endregion

        /// <summary>
        /// 设置UI参数
        /// </summary>
        /// <param name="uiParms"></param>
        public void SetUIWhenUIOpen(params object[] uiParms)
		{
			SetUI(uiParms);
			//开启协程
		}
		//协程loadDate 未完成
	
		public void Release()
		{
			this.State = EnumObjState.Closed;
			onRelease();
		}

		void OnDestroy()
		{
			onDestroy();
		}
		
	}
}
